/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : my_know.js                            
*  Description  : 마이홈 알림
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-05-12  김효순    Created.  
*  2012-05-12  김효순    Complete.
*******************************************************************************/

jQuery(function($){
	// 오늘알림
	function clsTodayAlarmList()
	{
		this._HTMLDom = null;
		this.listData = null;
		
		this._init();
	}
	
	clsTodayAlarmList.prototype = {
		_init: function(){
			var $layer = $("div#container section#my_home_left tbody");
			this._HTMLDom = {
				"$layer": $layer
				, "$element": $layer.find("tr:first").clone(true)
			}
			
			this.setList([]);
		}
		, setList: function(thisData){
			this.listData = thisData;
			this.display();
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			var $element = this._HTMLDom["$element"];
			var thisClass = this;
			
			$layer.empty();
			for(var i=0;i<this.listData.length;i++){
				$layer.append($element.clone(true));
			}
			$layer.find("tr").each(function(n){
				$(this).find("td:eq(0)").html(thisClass.listData[n]["알림내용"]);
				$(this).find("td:eq(1)").html(thisClass.listData[n]["날짜"]);
				$(this).find("td:eq(2)").html(thisClass.listData[n]["시간"]);
				
				switch(thisClass.listData[n]["알림타입"]){
					case "002":
						$(this).find("td:eq(0)").textWrap(thisClass.listData[n]["숫자"]+"명", "<span />");
					case "001": 
					case "003":
						$(this).find("td:eq(0)").textWrap(thisClass.listData[n]["언급자"]["닉네임"], "<span />");
						break;
				}
			});
		}
		, spinStart: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.hide();
			if($layer.parent("table").next("div#tmpSpin").length == 0){
				$layer.parent("table").after("<div id='tmpSpin' style='position:relative; width:500px; height:100px;' />");
			}
			$layer.parent("table").next("div#tmpSpin").show().spin();
		}
		, spinEnd: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.parent("table").next("div#tmpSpin").hide().spin(false);
			$layer.show();
		}
	}
	
	// 어제이후알림
	function clsPastAlarmList()
	{
		this._HTMLDom = null;
		this.listData = null;
		
		this._init();
	}
	
	clsPastAlarmList.prototype = {
		_init: function(){
			var $layer = $("div#container section#my_home_right tbody");
			this._HTMLDom = {
				"$layer": $layer
				, "$element": $layer.find("tr:first").clone(true)
			}
			
			this.setList([]);
		}
		, setList: function(thisData){
			this.listData = thisData;
			this.display();
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			var $element = this._HTMLDom["$element"];
			var thisClass = this;
			
			$layer.empty();
			for(var i=0;i<this.listData.length;i++){
				$layer.append($element.clone(true));
			}
			$layer.find("tr").each(function(n){
				$(this).find("td:eq(0)").html(thisClass.listData[n]["알림내용"]);
				$(this).find("td:eq(1)").html(thisClass.listData[n]["날짜"]);
				$(this).find("td:eq(2)").html(thisClass.listData[n]["시간"]);
				
				switch(thisClass.listData[n]["알림타입"]){
					case "002":
						$(this).find("td:eq(0)").textWrap(thisClass.listData[n]["숫자"]+"명", "<span />");
					case "001": 
					case "003":
						$(this).find("td:eq(0)").textWrap(thisClass.listData[n]["언급자"]["닉네임"], "<span />");
						break;
				}
			});
		}
		, spinStart: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.hide();
			if($layer.parent("table").next("div#tmpSpin").length == 0){
				$layer.parent("table").after("<div id='tmpSpin' style='position:relative; width:500px; height:100px;' />");
			}
			$layer.parent("table").next("div#tmpSpin").show().spin();
		}
		, spinEnd: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.parent("table").next("div#tmpSpin").hide().spin(false);
			$layer.show();
		}
	}
	
	// 서버통신
	function clsComm()
	{
		// 오늘알림
		this.todayAlarmList = null;
		// 어제이후알림
		this.pastAlarmList = null;
		
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){
			this.todayAlarmList = [
				{
					"알림키": null
					, "알림타입": null
					, "알림내용": null
					, "언급자": {
						"아이디": null
						, "이미지": null
						, "닉네임": null
					}
					, "숫자": null
					, "날짜": null
					, "시간": null
				}
			];
			
			this.pastAlarmList = [
				{
					"알림키": null
					, "알림타입": null
					, "알림내용": null
					, "언급자": {
						"아이디": null
						, "이미지": null
						, "닉네임": null
					}
					, "숫자": null
					, "날짜": null
					, "시간": null
				}
			];
		}
		, getTodayAlarmList: function(){
			var thisClass = this;
			
			//ajax
			setTimeout(function(){
				thisClass.todayAlarmList = [
					{
						"알림키": "001"
						, "알림타입": "001"
						, "알림내용": "몬유병님이 회원님의 글에 댓글을 남겼습니다."
						, "언급자": {
							"아이디": null
							, "이미지": null
							, "닉네임": "몬유병"
						}
						, "숫자": null
						, "날짜": "2012.03.01"
						, "시간": "18:01"
					}
					, {
						"알림키": "002"
						, "알림타입": "002"
						, "알림내용": "박아그나르트님 외 1명이 회원님의 글에 댓글을 남기셨습니다."
						, "언급자": {
							"아이디": null
							, "이미지": null
							, "닉네임": "박아그나르트"
						}
						, "숫자": "1"
						, "날짜": "2012.03.02"
						, "시간": "18:02"
					}
					, {
						"알림키": "003"
						, "알림타입": "003"
						, "알림내용": "몬유병님이 회원님의 조인룸에 참여하였습니.."
						, "언급자": {
							"아이디": null
							, "이미지": null
							, "닉네임": "몬유병"
						}
						, "숫자": null
						, "날짜": "2012.03.03"
						, "시간": "18:03"
					}
				];
				
				document.objControl.finishTodayAlarmList();
			}, 1500);
		}
		, getTodayAlarmData: function(){
			return this.todayAlarmList;
		}
		, getPastAlarmList: function(){
			var thisClass = this;
			
			//ajax
			setTimeout(function(){
				thisClass.pastAlarmList = [
					{
						"알림키": "001"
						, "알림타입": "001"
						, "알림내용": "몬유병님이 회원님의 글에 댓글을 남겼습니다."
						, "언급자": {
							"아이디": null
							, "이미지": null
							, "닉네임": "몬유병"
						}
						, "숫자": null
						, "날짜": "2012.03.01"
						, "시간": "18:01"
					}
					, {
						"알림키": "002"
						, "알림타입": "002"
						, "알림내용": "박아그나르트님 외 1명이 회원님의 글에 댓글을 남기셨습니다."
						, "언급자": {
							"아이디": null
							, "이미지": null
							, "닉네임": "박아그나르트"
						}
						, "숫자": "1"
						, "날짜": "2012.03.02"
						, "시간": "18:02"
					}
					, {
						"알림키": "003"
						, "알림타입": "003"
						, "알림내용": "몬유병님이 회원님의 조인룸에 참여하였습니.."
						, "언급자": {
							"아이디": null
							, "이미지": null
							, "닉네임": "몬유병"
						}
						, "숫자": null
						, "날짜": "2012.03.03"
						, "시간": "18:03"
					}
				];
				
				document.objControl.finishPastAlarmList();
			}, 1500);
		}
		, getPastAlarmData: function(){
			return this.pastAlarmList;
		}
	}
	
	// 컨트롤
	function clsControl()
	{
		this.objComm = null;
		this.objTodayAlarmList = null;
		this.objPastAlarmList = null;
		
		this._init();
	}
	
	clsControl.prototype = {
		_init: function(){
			this.objComm = new clsComm();
			this.objTodayAlarmList = new clsTodayAlarmList();
			this.objPastAlarmList = new clsPastAlarmList();
			
			this._bind();
			
			// 오늘알림
			this.objTodayAlarmList.spinStart();
			this.objComm.getTodayAlarmList();
			
			// 어제이후알림
			this.objPastAlarmList.spinStart();
			this.objComm.getPastAlarmList();
		}
		, _bind: function(){
			var thisClass = this;
		}
		// 오늘알림 완료
		, finishTodayAlarmList: function(){
			var thisData = this.objComm.getTodayAlarmData();
				
			this.objTodayAlarmList.spinEnd();
			this.objTodayAlarmList.setList(thisData);
		}
		// 어제이후알림 완료
		, finishPastAlarmList: function(){
			var thisData = this.objComm.getPastAlarmData();
				
			this.objPastAlarmList.spinEnd();
			this.objPastAlarmList.setList(thisData);
		}
	}
	
	document.objControl = new clsControl();
});
